<?php

namespace addons\mental\library;

use fast\Http;
use think\Cache;
use think\Log;
use addons\mental\model\WxUser;

/**
 * 微信小程序接口
 *
 */
class Qq
{
    private $app_id = '';
    private $app_secret = '';

    public function __construct()
    {
        $config = get_addon_config('mental');
        if (isset($config['qq_app_id'])) {
            $this->app_id = $config['qq_app_id'];
        }
        if (isset($config['qq_app_secret'])) {

            $this->app_secret = $config['qq_app_secret'];
        }
    }

    /**
     * Undocumented function
     * 根据前台传过来的code 去取openid和session_key
     * @param [type] $code
     * @return void
     */
    public function codeToSessionkey($code)
    {
        $result = [
            'status' => false,
            'data' => '',
            'msg' => '获取成功'
        ];
        $url = 'https://api.q.qq.com/sns/jscode2session?appid=' . $this->app_id . '&secret=' . $this->app_secret . '&js_code=' . $code . '&grant_type=authorization_code';
        $Http = new Http();
        $re = $Http->get($url);      
        $re = json_decode($re, true);
        if ($re['errcode']==0) {
            $result['data'] = $re;
            $result['status'] = true;
        } else {
            $result['msg'] = $re['errcode'] . ":" . $re['errmsg'];
        }
        return $result;
    }

}
